package com.linsen.erp.admin.controller;

import com.linsen.erp.admin.provider.dto.MenuDTO;
import com.linsen.erp.admin.provider.vo.MenuVO;
import com.linsen.erp.admin.service.IMenuService;
import com.linsen.core.common.util.ConvertUtil;
import com.linsen.core.common.util.Result;
import com.linsen.core.log.annotation.Log;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

/**
 * <p>
 * 菜单 前端控制器
 * </p>
 *
 * @author zhaoyong
 * @since 2023-07-24
 */
@RestController
@RequestMapping("/admin/menu")
@Api(tags = "菜单管理")
@RequiredArgsConstructor
public class MenuController {
    private final IMenuService iMenuService;

    @GetMapping("getPermissions")
    @ApiOperation(value = "当前登录用户的权限标识列表", response = String[].class)
    public Result getPermissions(){
        return Result.data(iMenuService.getUserPermissions());
    }

    @GetMapping("nav")
    @ApiOperation(value = "导航菜单列表（树形结构）", response = MenuVO[].class)
    public Result nav() {
        return Result.data(iMenuService.getUserMenuNavList());
    }

    @GetMapping("list")
    @ApiOperation(value = "菜单列表（树形结构）", response = MenuVO[].class)
    public Result list() {
        return Result.data(iMenuService.getMenuList());
    }

    @GetMapping("info")
    @ApiImplicitParam(name = "id", value = "菜单id", required = true, dataType = "string")
    @ApiOperation(value = "菜单详情", response = MenuVO.class)
    public Result get(@ApiIgnore @RequestParam(value = "id") String id) {
        return Result.data(ConvertUtil.convert(iMenuService.getById(id),MenuVO.class));
    }

    //@SaCheckPermission("admin:menu:save")
    @Log(value = "新增菜单")
    @PostMapping("save")
    @ApiOperation("新增菜单")
    public Result save(@RequestBody MenuDTO menuDTO) {
        iMenuService.saveMenu(menuDTO);

        return Result.ok();
    }

    //@SaCheckPermission("admin:menu:update")
    @Log(value = "修改菜单")
    @PostMapping("update")
    @ApiOperation("修改菜单")
    public Result update(@RequestBody MenuDTO menuDTO) {
        iMenuService.updateMenu(menuDTO);

        return Result.ok();
    }

    //@SaCheckPermission("admin:menu:delete")
    @Log(value = "删除菜单（单删）")
    @PostMapping("delete")
    @ApiImplicitParam(name = "id", value = "菜单id", required = true, dataType = "string")
    @ApiOperation("删除菜单（仅删除当前菜单信息）")
    public Result delete(@ApiIgnore @RequestParam("id") String id) {
        iMenuService.delete(id);

        return Result.ok();
    }

    //@SaCheckPermission("admin:menu:fetchDelete")
    @Log(value = "删除菜单（级联删除）")
    @PostMapping("fetchDelete")
    @ApiImplicitParam(name = "id", value = "菜单id", required = true, dataType = "string")
    @ApiOperation("删除菜单（同时级联删除全部子级菜单）")
    public Result fetchDelete(@ApiIgnore @RequestParam("id") String id) {
        iMenuService.fetchDelete(id);

        return Result.ok();
    }

    //@SaCheckPermission("admin:menu:copy")
    @Log(value = "复制菜单（单复制）")
    @PostMapping("copy")
    @ApiImplicitParam(name = "id", value = "菜单id", required = true, dataType = "string")
    @ApiOperation("复制菜单（仅复制当前菜单信息，以'COPY'结尾）")
    public Result copy(@ApiIgnore @RequestParam("id") String id) {
        iMenuService.copy(id);

        return Result.ok();
    }

    //@SaCheckPermission("admin:menu:fetchCopy")
    @Log(value = "复制菜单（级联复制）")
    @PostMapping("fetchCopy")
    @ApiImplicitParam(name = "id", value = "菜单id", required = true, dataType = "string")
    @ApiOperation("复制菜单（同时级联复制全部子级菜单，仅当前菜单以'COPY'结尾）")
    public Result fetchCopy(@ApiIgnore @RequestParam("id") String id) {
        iMenuService.fetchCopy(id);

        return Result.ok();
    }
}
